// StableSupervisorStrategy.java
package com.stable.actor.core;

import lombok.extern.slf4j.Slf4j;


import java.time.Duration;
import java.util.function.Function;

/**
 * Stable Actor Framework 监督策略
 * 类似 Akka 的 SupervisorStrategy 功能
 */
@Slf4j
public class StableSupervisorStrategy {


    private final int maxRetries;
    private final Duration withinTimeRange;
    private final boolean loggingEnabled;
    private final Function<Throwable, Directive> decider;

    public StableSupervisorStrategy(int maxRetries, Duration withinTimeRange,
                                    boolean loggingEnabled, Function<Throwable, Directive> decider) {
        this.maxRetries = maxRetries;
        this.withinTimeRange = withinTimeRange;
        this.loggingEnabled = loggingEnabled;
        this.decider = decider;
    }

    public static StableSupervisorStrategy create() {
        return new Builder().build();
    }

    public Directive decide(Throwable cause) {
        if (loggingEnabled) {
            log.warn("Supervisor strategy handling exception: {}", cause.getClass().getSimpleName(), cause);
        }
        return decider.apply(cause);
    }

    public int getMaxRetries() {
        return maxRetries;
    }

    public Duration getWithinTimeRange() {
        return withinTimeRange;
    }

    public boolean isLoggingEnabled() {
        return loggingEnabled;
    }

    // 监督策略指令
    public enum Directive {
        RESUME,    // 继续处理下一条消息
        RESTART,   // 重启 Actor
        STOP,      // 停止 Actor
        ESCALATE   // 将异常上报
    }

    // 便捷方法
    public static Directive resume() {
        return Directive.RESUME;
    }

    public static Directive restart() {
        return Directive.RESTART;
    }

    public static Directive stop() {
        return Directive.STOP;
    }

    public static Directive escalate() {
        return Directive.ESCALATE;
    }

    // Builder 模式
    public static class Builder {
        private int maxRetries = -1; // -1 表示无限重试
        private Duration withinTimeRange = Duration.ofMinutes(1);
        private boolean loggingEnabled = true;
        private Function<Throwable, Directive> decider = new DefaultDecider();

        public Builder withMaxRetries(int maxRetries) {
            this.maxRetries = maxRetries;
            return this;
        }

        public Builder withWithinTimeRange(Duration withinTimeRange) {
            this.withinTimeRange = withinTimeRange;
            return this;
        }

        public Builder withLoggingEnabled(boolean loggingEnabled) {
            this.loggingEnabled = loggingEnabled;
            return this;
        }

        public Builder withDecider(Function<Throwable, Directive> decider) {
            this.decider = decider;
            return this;
        }

        public StableSupervisorStrategy build() {
            return new StableSupervisorStrategy(maxRetries, withinTimeRange, loggingEnabled, decider);
        }
    }

    // 默认决策器
    private static class DefaultDecider implements Function<Throwable, Directive> {
        @Override
        public Directive apply(Throwable throwable) {
            // 默认策略：对所有异常都重启
            return Directive.RESTART;
        }
    }
}
